package cn.liccshop.core.service.shopGoodsComment.service;

import cn.liccshop.core.entity.*;
import cn.liccshop.core.proscenium.shopUser.mapper.ShopUserMapper;
import cn.liccshop.core.service.shopGoods.mapper.ShopGoodsMapper;
import cn.liccshop.core.service.shopGoodsComment.mapper.ShopGoodsCommentMapper;
import cn.liccshop.core.service.shopGoodsComment.service.IShopGoodsCommentService;
import cn.liccshop.core.service.shopOrderGoods.mapper.ShopOrderGoodsMapper;
import cn.liccshop.core.service.shopOrderInfo.mapper.ShopOrderInfoMapper;
import cn.liccshop.sys.constants.Constants;
import cn.liccshop.sys.exception.MySystemException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品评论表 服务实现类
 * </p>
 *
 * @author LiCC
 * @since 2019-05-13
 */
@Service
public class ShopGoodsCommentServiceImpl extends ServiceImpl<ShopGoodsCommentMapper, ShopGoodsComment> implements IShopGoodsCommentService {

    @Autowired
    private ShopOrderGoodsMapper orderGoodsMapper;
    @Autowired
    private ShopOrderInfoMapper orderInfoMapper;
    @Autowired
    private ShopUserMapper userMapper;
    @Autowired
    private ShopGoodsMapper goodsMapper;

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void orderCommentSubimt(ShopGoodsComment goodsComment, String orderGoodsId) throws MySystemException {
        ShopUser user = userMapper.selectById(goodsComment.getMemberId());
        /** 保存评论信息 **/
        baseMapper.insert(goodsComment.setMemberName(user.getNickname()));
        /** 更新订单商品信息状态 **/
        orderGoodsMapper.updateById(new ShopOrderGoods().setId(orderGoodsId).setIsComment(1));
        /** 增加商品评论数 **/
        goodsMapper.updateById(new ShopGoods().setId(goodsComment.getGoodsId()).setCommentsNumber(goodsMapper.selectById(goodsComment.getGoodsId()).getCommentsNumber() + 1));
        /** 获取该订单下的订单商品信息 **/
        List<ShopOrderGoods> orderGoodsList = orderGoodsMapper.selectList(new QueryWrapper<>(new ShopOrderGoods().setOrderId(goodsComment.getOrderId())));
        boolean isComment = true;
        for (ShopOrderGoods orderGoods: orderGoodsList){
            /** 查询该订单下的商品是否都评价了 **/
            if (orderGoods.getIsComment() == 0){
                isComment = false;
            }
        }
        /** 订单下的商品都评价了，更新商品为完成状态 **/
        if (isComment){
            orderInfoMapper.updateById(new ShopOrderInfo().setId(goodsComment.getOrderId()).setOrderStatus(Constants.ORDER_STATUS_SUCCESS));
        }
    }

    @Override
    public Map<String, Object> liccCommontInfoByPage(Map<String, Object> map) {
        Integer total = baseMapper.liccCommontInfoCount(map);
        List<Map<String, Object>> records = baseMapper.liccCommontInfoByPage(map);
        Map<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("records", records);
        data.put("searchCount", true);
        return data;
    }
}
